package net.tatu.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.tatu.dao.OrgBusinessTargetRelaMapper;
import net.tatu.dao.PubCodeMapper;
import net.tatu.dao.SysLogMapper;
import net.tatu.dao.UserLevelRangeDefineMapper;
import net.tatu.dao.UserMapper;
import net.tatu.dao.UserPointDetailMapper;
import net.tatu.dao.UserPremAccountMapper;
import net.tatu.dao.UserPremDetailMapper;
import net.tatu.model.OrgBusinessTargetRela;
import net.tatu.model.PubCode;
import net.tatu.model.SysLog;
import net.tatu.model.User;
import net.tatu.model.UserLevelRangeDefine;
import net.tatu.model.UserPointDetail;
import net.tatu.model.UserPremAccount;
import net.tatu.model.UserPremDetail;
import net.tatu.service.OrgBusinessTargetRelaService;
import net.tatu.service.PubCodeService;
import net.tatu.service.SysLogService;
import net.tatu.service.UserPointService;
import net.tatu.service.UserPremAccountService;
import net.tatu.service.UserService;
import net.tatu.util.Encrypt;
import net.tatu.util.Util;

@Service
public class UserPointServiceImpl implements UserPointService {
	Logger log = Logger.getLogger(UserPointServiceImpl.class);
	@Autowired
	private UserPremAccountMapper userPremAccountMapper;
	@Autowired
	private UserPremDetailMapper userPremDetailMapper;
	@Autowired
	private UserLevelRangeDefineMapper userLevelRangeDefineMapper;
	@Autowired
	private UserPointDetailMapper userPointDetailMapper;
	@Autowired
	private UserPremAccountService userPremAccountService;
	@Autowired
	private UserService userService;
	@Override
	public boolean pointConvertCash(User user, int point) {
		boolean ret = false;
		//减掉用户积分并记录到流水表
		user.setUserPoint((user.getUserPoint() == null ? new BigDecimal(point) : new BigDecimal(user.getUserPoint()).subtract(new BigDecimal(point))).doubleValue());
		boolean a = newPointDetail(point, 114001, 10402, user.getUserId());
		//增加用户资金并记录到流水表
		//获取用户资金账户
		UserPremAccount userPremAccount = new UserPremAccount();
		userPremAccount.setUserPremActId(user.getUserId());
		UserPremAccount upa = userPremAccountService.getUserPremAccount(userPremAccount);
		upa.setUserPointPrem((upa.getUserPointPrem() == null ? new BigDecimal(point * 10) : new BigDecimal(upa.getUserPointPrem()).add(new BigDecimal(point * 10))).doubleValue());
		boolean b = userPremAccountService.newPremDetail(user.getUserId(), point * 10, 10401, 114001);
		userPremAccountService.updateUserPremAccount(upa);
		userService.updateUser(user);
		if(a && b){
			ret = true;
		}
		return ret;
	}
	
	
	
	public boolean newPointDetail(int changePoint, int pointChangeStatusCode, int changeTypeCode, long userPremActId){
		boolean ret = false;
		UserPointDetail userPointDetail = new UserPointDetail();
		userPointDetail.setChangePoint(Double.valueOf(changePoint));
		userPointDetail.setRecordStatusCode(1);
		userPointDetail.setChangeTypeCode(changeTypeCode);
		userPointDetail.setPointChangeStatusCode(pointChangeStatusCode);
		userPointDetail.setUserPremActId(userPremActId);
		userPointDetail.setPointToRMB(Double.valueOf(10));
		userPointDetail.setPointPrem(Double.valueOf(changePoint * 10));
		int i = userPointDetailMapper.insertUserPointDetail(userPointDetail);
		if(i == 1){
			ret = true;
		}
		return ret;
	}
}
